package cn.smile.controller.admin;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import cn.smile.bean.CodeMsg;
import cn.smile.bean.PageBean;
import cn.smile.bean.Result;
import cn.smile.entity.admin.Menu;
import cn.smile.entity.admin.Role;
import cn.smile.service.admin.MenuService;
import cn.smile.service.admin.OperaterLogService;
import cn.smile.service.admin.RoleService;
import cn.smile.util.MenuUtil;
import cn.smile.util.ValidateEntityUtil;

import javax.annotation.Resource;

/**
 * 后台角色管理控制器
 */
@Slf4j
@RequestMapping("/admin/role")
@Controller
public class RoleController {

    @Resource
    private MenuService menuService;

    @Resource
    private OperaterLogService operaterLogService;

    @Resource
    private RoleService roleService;

    /**
     * 分页搜索角色列表
     */
    @RequestMapping(value = "/list")
    public String list(Model model, Role role, PageBean<Role> pageBean) {
        model.addAttribute("title", "角色列表");
        model.addAttribute("name", role.getName());
        model.addAttribute("pageBean", roleService.findByName(role, pageBean));
        return "admin/role/list";
    }

    /**
     * 角色添加页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        model.addAttribute("thirdMenus", MenuUtil.getThirdMenus(findAll));
        return "admin/role/add";
    }

    /**
     * 角色添加表单提交处理
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> add(Role role) {
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(role);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        if (roleService.save(role) == null) {
            return Result.error(CodeMsg.ADMIN_ROLE_ADD_ERROR);
        }
        log.info("添加角色【{}】", role);
        operaterLogService.add("添加角色【" + role.getName() + "】");
        return Result.success(true);
    }

    /**
     * 角色编辑页面
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(@RequestParam(name = "id") Long id, Model model) {
        List<Menu> findAll = menuService.findAll();
        model.addAttribute("topMenus", MenuUtil.getTopMenus(findAll));
        model.addAttribute("secondMenus", MenuUtil.getSecondMenus(findAll));
        model.addAttribute("thirdMenus", MenuUtil.getThirdMenus(findAll));
        Role role = roleService.find(id);
        model.addAttribute("role", role);
        model.addAttribute("authorities", JSONArray.toJSON(role.getAuthorities()).toString());
        return "admin/role/edit";
    }

    /**
     * 角色修改表单提交处理
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> edit(Role role) {
        //用统一验证实体方法验证是否合法
        CodeMsg validate = ValidateEntityUtil.validate(role);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        Role existRole = roleService.find(role.getId());
        if (existRole == null) {
            return Result.error(CodeMsg.ADMIN_ROLE_NO_EXIST);
        }
        existRole.setName(role.getName());
        existRole.setRemark(role.getRemark());
        existRole.setStatus(role.getStatus());
        existRole.setAuthorities(role.getAuthorities());
        if (roleService.save(existRole) == null) {
            return Result.error(CodeMsg.ADMIN_ROLE_EDIT_ERROR);
        }
        log.info("编辑角色【{}】", role);
        operaterLogService.add("编辑角色【" + role.getName() + "】");
        return Result.success(true);
    }

    /**
     * 删除角色
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id") Long id) {
        try {
            roleService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_ROLE_DELETE_ERROR);
        }
        log.info("编辑角色ID【{}】", id);
        operaterLogService.add("删除角色ID【" + id + "】");
        return Result.success(true);
    }
}
